package com.song.tools.songtest.manager;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.song.tools.core.util.ObjectUtils;
import com.song.tools.mvc.manager.BaseManager;
import com.song.tools.songtest.beans.bo.SysRoleUserBo;
import com.song.tools.songtest.beans.query.SysRoleUserQuery;
import com.song.tools.songtest.convertor.SysRoleUserConvertor;
import com.song.tools.songtest.dao.entity.SysRoleUser;
import com.song.tools.songtest.dao.mapper.SysRoleUserMapper;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 用户角色关系表 管理类
 * </p>
 *
 * @author song tools
 * @since 2024-07-09
 */
@Component
public class SysRoleUserManager extends BaseManager<SysRoleUser, SysRoleUserBo, SysRoleUserMapper, SysRoleUserQuery, Long, SysRoleUserConvertor> {

    @Override
    public LambdaQueryWrapper<SysRoleUser> encapsulateQueryWrapper(SysRoleUserQuery query) {
        LambdaQueryWrapper<SysRoleUser> queryWrapper = super.encapsulateQueryWrapper(query);
        if (query == null) {
            return queryWrapper;
        }

        // 组装查询条件
        queryWrapper.eq(ObjectUtils.isNotEmpty(query.getRoleId()), SysRoleUser::getRoleId, query.getRoleId());
        queryWrapper.eq(ObjectUtils.isNotEmpty(query.getUserId()), SysRoleUser::getUserId, query.getUserId());
        queryWrapper.eq(ObjectUtils.isNotEmpty(query.getRemark()), SysRoleUser::getRemark, query.getRemark());
        queryWrapper.ge(ObjectUtils.isNotEmpty(query.getCreateTimeStart()), SysRoleUser::getCreateTime, query.getCreateTimeStart());
        queryWrapper.le(ObjectUtils.isNotEmpty(query.getCreateTimeEnd()), SysRoleUser::getCreateTime, query.getCreateTimeEnd());
        queryWrapper.ge(ObjectUtils.isNotEmpty(query.getUpdateTimeStart()), SysRoleUser::getUpdateTime, query.getUpdateTimeStart());
        queryWrapper.le(ObjectUtils.isNotEmpty(query.getUpdateTimeEnd()), SysRoleUser::getUpdateTime, query.getUpdateTimeEnd());

        return queryWrapper;
    }

    /**
     * 根据用户ID删除
     *
     * @param userId 用户ID
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteByUserId(Long userId) {
        LambdaUpdateWrapper<SysRoleUser> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SysRoleUser::getUserId, userId);
        mapper.delete(updateWrapper);
    }
}
